#include<iostream>
#include"TcpServer.hpp"
#include<memory>
#include"Socket.hpp"
#include"Accepter.hpp"
#include"Comm.hpp"

void test(Connection* listensock)
{
    std::cout<<"get event,sockfd is: "<<listensock->GetFd()<<std::endl;
}

const int defaultbacklog=32;
int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        std::cout<<"Usage: "<<argv[0]<<":port"<<std::endl;
    }
    uint16_t port=std::stoi(argv[1]);
    //1.创建listen套接字
    auto listensock=std::make_unique<TcpSocket>();
    listensock->BuildListenSocketMethod(port,defaultbacklog);
    lg.LogMessage(Info,"create listensock success: %d\n",listensock->GetSocket());
    auto accept=std::make_unique<Accepter>();
    //2.创建tcpserver
    auto svr=std::make_unique<TcpServer>();
    //3.构建listen对应的connection,加入到tcpserver中
    auto accrecv=std::bind(&Accepter::AcceptConnection,accept.get(),std::placeholders::_1);
    auto conn=ConnectionFactory::BuildListenConnection(listensock->GetSocket(),accrecv,EPOLLIN|EPOLLET,svr.get());
    svr->AddConnection(conn);
    svr->Dispatcher();//任务分配器
    return 0;
}